//
//  NovelAlbumHeaderView.m
//  A_program
//
//  Created by dlios on 15-5-23.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "NovelAlbumHeaderView.h"
#import "NovelAlbumHeaderModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "MyAlbumFirstButton.h"
#import "NovelModel.h"
#import "NovelDatabase.h"


@implementation NovelAlbumHeaderView

- (void)dealloc
{
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        [self creatAlbumHeaderView];
    }
    return self;
}
- (void)creatAlbumHeaderView{
    self.albumImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 25, [UIScreen mainScreen].bounds.size.width/3 - 20, [UIScreen mainScreen].bounds.size.width/3 - 20)];
    [self addSubview:self.albumImageView];
    [self.albumImageView release];
    
    self.albumFristImageView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3, 25, 30, 30)];
    self.albumFristImageView.layer.cornerRadius = 15;
    self.albumFristImageView.layer.masksToBounds = YES;
    [self addSubview:self.albumFristImageView];
    [self.albumFristImageView release];
    
    self.albumLabel = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3 + 30, 27.5, 150, 20)];
    [self addSubview:self.albumLabel];
     [self.albumLabel release];
    
    self.albumFristButton = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height*80/677, [UIScreen mainScreen].bounds.size.width*2/3 - 40, 20)];
    [self addSubview:self.albumFristButton];
    self.albumFristButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.albumFristButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.albumFristButton addTarget:self action:@selector(albumFristButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.albumFristButton release];
    
    self.albumEnterImageView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width -40, [UIScreen mainScreen].bounds.size.height*80/677, 20, 20)];
    [self.albumEnterImageView setImage:[UIImage imageNamed:@"enter.png"]];
    self.albumEnterImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(albumFristButtonAction:)];
    [self.albumEnterImageView addGestureRecognizer:tap];
    [self addSubview:self.albumEnterImageView];
    
    //收藏
#pragma mark -
#pragma mark 收藏按钮
    self.albumFristNextButton = [[MyAlbumFirstButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 60, [UIScreen mainScreen].bounds.size.height*3/16-10, 50, 20)];
    self.albumFristNextButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [self.albumFristNextButton setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
    [self addSubview:self.albumFristNextButton];
    [self.albumFristNextButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.albumFristNextButton addTarget:self action:@selector(albumFristNextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }

- (void)albumFristNextButtonAction:(id)sender{
#pragma mark-
#pragma mark 判断是否已收藏
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSInteger myAlbumId = self.albumFristNextButton.albumId;
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:myAlbumId loginName:[myLoginName objectForKey:@"loginName"]];
    if ([array count] == 0 && [self.albumFristNextButton.titleLabel.text isEqualToString:@"收藏"]) {
        
        NSString *collect = [NSString stringWithFormat:@"%ld",self.albumFristNextButton.albumId];
        [self.myAlbumFirstButtonDelegate createCollectAction:collect];
        
        if (![[myLoginName objectForKey:@"loginName"] length] == 0 &&[[myLoginName objectForKey:@"state"]isEqualToString:@"登录"]) {
            [self.albumFristNextButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }else{
            NSLog(@"未登录或则没有用户");
            
            
        }

    }else if([self.albumFristNextButton.titleLabel.text isEqualToString:@"已收藏"]){
        
        NovelModel *myid = [[NovelModel alloc]init];
        for (NovelModel *model in array) {
            myid = model;
        }
        [NovelDatabase deleteWithModel:myid];
        [self.albumFristNextButton setTitle:@"收藏" forState:UIControlStateNormal];
        
    }else{
        NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:self.albumFristNextButton.albumId  loginName:[myLoginName objectForKey:@"loginName"]];
        if ( ![array count] == 0) {
            [self.albumFristNextButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }
    }
    NSLog(@"点击");
    
}



//传值
- (void)createHeaderView:(NovelAlbumHeaderModel *)albumHeaderModel{
    
    [self.albumImageView sd_setImageWithURL:[NSURL URLWithString:albumHeaderModel.coverOrigin] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    
    [self.albumFristImageView sd_setImageWithURL:[NSURL URLWithString:albumHeaderModel.avatarPath] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    
    self.albumLabel.text = albumHeaderModel.nickname;
    
    [self.albumFristButton setTitle:albumHeaderModel.intro forState:UIControlStateNormal];
     self.albumFristNextButton.albumId = albumHeaderModel.albumId;

  
}
- (void)albumFristButtonAction:(id)sender{
    [self.albumChangeDelegate albumChangeViewController];
}

@end
